package com.czn.controller;

import com.czn.entity.Bill;
import com.czn.entity.BillType;
import com.czn.entity.ResultV0;
import com.czn.service.BillService;
import com.czn.service.TypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController//不要写成@Controller！！！
//@RestController注解，相当于@Controller+@ResponseBody两个注解的结合，返回json数据
//使用@Controller 注解，在对应的方法上，视图解析器可以解析return 的jsp,html页面，并且跳转到相应页面
@RequestMapping("/bill")
@RefreshScope //配置文件改变，自动刷新，Spring cloud bus
public class BillController {
    @Autowired
    private TypeService typeService;

    @Autowired
    private BillService billService;

    @Value("${test.name}")//通过该注释读取配置文件中的信息
    private String name;//用于测试码云中配置文件的更新

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param date1
     * @param date2
     * @param typeId
     * @return
     */
    @RequestMapping("/list-page")
    public String listPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int limit,
            Date date1,
            Date date2,
            String typeId
    ) {
        Bill bill = new Bill();
        if(typeId != null && typeId.trim().length()!=0){
            bill.setTypeId(new Long(typeId));
        }
        bill.setDate1(date1);
        bill.setDate2(date2);
        System.out.println("搜索条件："+bill);
        System.out.println("page:"+page+",limit:"+limit);
        PageInfo<Bill> pageInfo = billService.listPage(bill, page, limit);
        List<Bill> list = pageInfo.getList();
        ResultV0 resultV0 = new ResultV0();
        resultV0.setData(list);
        resultV0.setCode(0);
        resultV0.setCount(pageInfo.getTotal());
        String s = JSON.toJSONString(resultV0);
        //System.out.println(s);
        return s;
    }


    @RequestMapping("/billTypes")
    public String billTypes(){
        List<BillType> types = typeService.list();
        ResultV0 resultV0 = new ResultV0();
        resultV0.setData(types);
        resultV0.setCode(0);
        resultV0.setCount(Integer.toUnsignedLong(types.size()));
        String s = JSON.toJSONString(resultV0);
        //System.out.println(s);
        return s;
    }

    /**
     * 该方法仅用于测试
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Bill queryById(@PathVariable Long id){
        System.out.println("配置文件中的test.name为："+name);//用于测试码云中配置文件的更新
        return billService.queryById(id);
    }


    /**
     * 查询
     * @param b
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Bill b, Model model){
        //获取所有记账类型，并存储在model中供前端使用
        List<BillType> types = typeService.list();
        model.addAttribute("types",types);

        //查询所有账单，并存储在model中供前端使用
        List<Bill> list = billService.list(b);
        model.addAttribute("bills", list);

        return "bill/list";
    }

    /**
     * 添加
     *
     * @param b
     * @return
     */
    @RequestMapping("/add")
    public String add(Bill b, String callback) {
        System.out.println(callback);
        System.out.println("新增的bill："+b);
        int add = billService.add(b);
        ResultV0 resultV0 = new ResultV0();
        if (add > 0) {
            resultV0.setMsg("1");
            String jsonData = JSON.toJSONString(resultV0);
            return callback + "(" + jsonData + ")";
        }
        resultV0.setMsg("0");
        String jsonData = JSON.toJSONString(resultV0);
        System.out.println(jsonData);
        return callback + "(" + jsonData + ")";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        billService.delete(id);
        return null;
    }

    /**
     * 修改
     *
     * @param b
     * @return
     */
    @RequestMapping("/update")
    public String update(Bill b, String callback) {
        System.out.println("修改后的账单信息："+b);
        int update = billService.update(b);
        ResultV0 resultV0 = new ResultV0();
        if (update > 0) {
            resultV0.setMsg("1");
            String jsonData = JSON.toJSONString(resultV0);
            return callback + "(" + jsonData + ")";
        }
        resultV0.setMsg("0");
        String jsonData = JSON.toJSONString(resultV0);
        return callback + "(" + jsonData + ")";

    }

}
